#include "infrared_ray.h"

INT8U ir_delay_x100us;

IR_T ir_t; 
/*****************************************************************************
 �� �� ��:ir_init
 ��������  : �����ʼ������
 �������  : ��      
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ     :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void ir_init()
{
    INTERRUPT_EXTERN0_FALLING();
    INTERRUPT_EXTERN0_ENABLE();
    SET_IR_DATA();

    ir_t.step = 0xFF;
    ir_t.data_pointer = 0;
}

/*****************************************************************************
 �� �� ��:ir_init
 ��������  : ������մ�������
 �������  : ��      
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ     :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void ir_data_process()
{
    switch (ir_t.step)
    {
        case 0:
        {
            if(0 == GET_IR_DATA())//���źŵ���
            {
                ir_t.delay_ms = 9;
                ir_t.step ++;
            }
        }
        break;
        case 1:
        {
            if(0 == GET_IR_DATA() && 0 == ir_t.delay_ms)//�ȴ�9ms�͵�ƽ��ȥ
            {
                ir_t.step ++;
            }
        }
        break;
        case 2:
        {
            if(1 == GET_IR_DATA())//��ȷ�ȵ�9ms�͵�ƽ
            {
                ir_delay_x100us = 4;
                ir_t.step ++;
            }
            else
            {
                ir_t.step = 0;
            }
        }
        break;
        case 3:
        {
            if(0 == GET_IR_DATA() && 0 == ir_delay_x100us)//��ȷ�ȵ�4.5ms�ߵ�ƽ
            {
                ir_delay_x100us = 5;
                ir_t.step ++;
            }
            else
            {
                ir_t.step = 0;
            }
        }
        break;
        case 4:
        {
            if(1 == GET_IR_DATA() && 0 == ir_delay_x100us) 
            {
                ir_delay_x100us = 56;
                ir_t.step ++; 
            }
        }
        break;
        case 5:
        {
            if(0 == ir_delay_x100us)
            {
                if(1 == GET_IR_DATA())
                {
                    ir_t.Data[ir_t.data_pointer] |= 0x80;
                }
                ir_t.Data[ir_t.data_pointer] >>= 1;
                ir_t.Bit ++;
                if(8 == ir_t.Bit)
                {
                    ir_t.data_pointer ++;
                    ir_t.Bit = 0;
                }
            }
            if(4 == ir_t.data_pointer)
            {
                ir_t.step = 0;
                ir_t.data_pointer = 0;
            }
        }
        break;
        default:
            break;
    }
}

/*****************************************************************************
 �� �� ��:ir_init
 ��������  : ��������жϴ�������
 �������  : ��      
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ     :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
void ir_recived() interrupt 0
{
    ir_t.delay_ms = 70; 
        ir_t.step   = 0;
}
